package com.xci.platform.auth.service;

import com.github.pagehelper.Page;
import com.xci.platform.annotation.OperateUser;
import com.xci.platform.annotation.QueryMap;
import com.xci.platform.auth.core.AuthConstant;
import com.xci.platform.auth.core.HistoryModel;
import com.xci.platform.auth.core.HistoryOperateType;
import com.xci.platform.auth.dao.ReleaseHistoryDao;
import com.xci.platform.auth.entity.ReleaseHistoryEntity;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.helper.CoreHelper;
import com.xci.platform.helper.StringHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 系统发布记录服务
 */
@Service
public class ReleaseHistoryService extends AuthBaseService {

    /**
     * 系统发布记录数据访问对象
     */
    @Resource
    private ReleaseHistoryDao releaseHistoryDao;

    /**
     * 保存系统发布记录
     *
     * @param entity   系统发布记录对象
     * @param isCreate 是否新建
     */
    @Validated
    @OperateUser
    @Transactional(rollbackFor = Exception.class)
    public BoolMessage save(ReleaseHistoryEntity entity, Boolean isCreate) {
        //系统发布记录名称验证
        if (releaseHistoryDao.existByVersion(entity.getVersion(), entity.getId())) {
            String msg = StringHelper.format("系统发布版本 {} 已经存在", entity.getVersion());
            return new BoolMessage(false, msg);
        }

        String msg;
        long startTime = CoreHelper.startWatch();
        if (isCreate) {
            msg = StringHelper.format("新增系统发布记录 {}", entity.getVersion());
            releaseHistoryDao.insert(entity);
            //region 历史记录
            operateHistory(buildHistory()
                    .operateType(HistoryOperateType.Insert)
                    .keyValue(entity.getId())
                    .message(msg)
                    .after(entity)
            );
            //endregion
        } else {
            msg = StringHelper.format("修改系统发布记录 {}", entity.getVersion());
            ReleaseHistoryEntity oldEntity = queryById(entity.getId());
            releaseHistoryDao.update(entity);
            //region 历史记录
            operateHistory(buildHistory()
                    .operateType(HistoryOperateType.Update)
                    .keyValue(entity.getId())
                    .message(msg)
                    .before(oldEntity)
                    .after(entity)
            );
            //endregion
        }
        // 操作日志
        operateLog(AuthConstant.SystemModule.ReleaseHistory, msg, CoreHelper.stopWatch(startTime));
        return BoolMessage.True;
    }

    /**
     * 删除系统发布记录
     *
     * @param id 系统发布记录主键数组
     */
    @Validated
    @Transactional(rollbackFor = Exception.class)
    public BoolMessage delete(@NotEmpty(message = "请指定删除的系统发布记录主键") String id) {
        ReleaseHistoryEntity oldEntity = releaseHistoryDao.queryById(id);
        if (oldEntity == null) return BoolMessage.True;

        long startTime = CoreHelper.startWatch();
        releaseHistoryDao.delete(id);
        String msg = StringHelper.format("删除系统发布记录 {}", oldEntity.getVersion());
        //region 历史记录
        operateHistory(buildHistory()
                .operateType(HistoryOperateType.Delete)
                .keyValue(id)
                .message(msg)
                .before(oldEntity)
        );
        //endregion
        // 操作日志
        operateLog(AuthConstant.SystemModule.ReleaseHistory, msg, CoreHelper.stopWatch(startTime));
        return BoolMessage.True;
    }

    /**
     * 根据系统发布记录主键获取系统发布记录对象
     *
     * @param id 系统发布记录主键
     * @return 返回指定系统发布记录主键的系统发布记录对象
     */
    @Validated
    public ReleaseHistoryEntity queryById(@NotNull(message = "请指定系统发布记录主键") String id) {
        return releaseHistoryDao.queryById(id);
    }

    /**
     * 查询系统发布记录列表
     *
     * @param params 查询系统发布记录
     * @return 返回符合查询条件的系统发布记录列表
     */
    @QueryMap
    public List<ReleaseHistoryEntity> query(Map params) {
        return releaseHistoryDao.query(params);
    }

    /**
     * 查询系统发布记录分页列表
     *
     * @param params 查询系统发布记录
     * @return 返回符合查询条件的系统发布记录分页列表
     */
    @QueryMap
    public Page<ReleaseHistoryEntity> queryPage(Map params) {
        return (Page<ReleaseHistoryEntity>) releaseHistoryDao.query(params);
    }

    /**
     * 生成历史记录对象
     */
    private HistoryModel.HistoryModelBuilder buildHistory() {
        return HistoryModel.builder()
                .tableName("sys_release_history")
                .tableCaption("系统发布记录")
                .keyName("id");
    }
}
